package min3d.sampleProject1;
import android.view.Menu;
import android.view.MenuItem;

public class MyMenu {
	private Menu menu;
	private MyActivity ra;
	public boolean 
		showModel = true,
		showInfo = true,
		trackingOn = false,
		interactive = false,
		tracker1 = true,
		tracker2 = false;
	private static final int 
		OPTION_1 = 1,  // MODEL
	 	OPTION_2 = 2,  // TRACKING
	 	OPTION_3 = 3,  // INFO
	 	OPTION_4 = 4,  // INTERACTIVE
	 	OPTION_5 = 5,  // TRACKER 1
		OPTION_6 = 6;  // TRACKER 2
	public TrackerThread tThread;
	
	/**
	 * MyMenu constructor.
	 * Saves the MyActivity handle and instantiates the tracker. 
	 * @param  MyActivity 			The MyActivity context.
	 */
	public MyMenu(MyActivity ra){
		this.ra = ra;
        tThread = new TrackerThread(this, ra);
	}
	
	/**
	 * Android hook for the menu.
	 * @param  Menu 			A handle to the menu.
	 * @return boolean			True if event handled, false otherwise.
	 */
	public boolean init(Menu menu){
    	this.menu = menu;
    	update(false);
    	return true;
	}
	
	/**
	 * Updates the state of the menu depending on the boolean values in the object.
	 * @param  boolean	Boolean saying if the menu items should be removed first. 
	 * @return void
	 */
	private void update(boolean remove){
		if(remove){
			menu.removeItem(OPTION_1);
			menu.removeItem(OPTION_2);
			menu.removeItem(OPTION_3);
			menu.removeItem(OPTION_4);
			menu.removeItem(OPTION_5);
			menu.removeItem(OPTION_6);
		}
		if(showModel)	menu.add(0,OPTION_1,0,"Hide model");
		else 			menu.add(0,OPTION_1,0,"Show model");
		if(trackingOn)  menu.add(0,OPTION_2,0,"Stop tracking");
		else			menu.add(0,OPTION_2,0,"Start tracking");
		if(showInfo)	menu.add(0,OPTION_3,0,"Hide info");
		else			menu.add(0,OPTION_3,0,"Show info");
		if(interactive)	menu.add(0,OPTION_4,0,"Stop interactivity");
		else			menu.add(0,OPTION_4,0,"Start interactivity");
		if(tracker1)	menu.add(0,OPTION_5,0,"Tracker 1 (x)");
		else			menu.add(0,OPTION_5,0,"Tracker 1");
		if(tracker2)	menu.add(0,OPTION_6,0,"Tracker 2 (x)");
		else			menu.add(0,OPTION_6,0,"Tracker 2");
	}
	
	/**
	 * Android hook that is called on a menu item selection event.
	 * @param  MenuItem		The MenuItem that was selected.
	 * @return boolean		True if event handled, false otherwise.
	 */
	public boolean select(MenuItem item){
    	switch (item.getItemId()){
    	case OPTION_1: // MODEL
    		showModel = showModel ? false : true;
    		update(true);
    		break;
    	case OPTION_2: // TRACKING
    		trackingOn = trackingOn ? false : true;
    		if(trackingOn)
    			tThread.start();
    		else{
    			tThread.interrupt();
    			tThread = new TrackerThread(this, ra);		// newly added
    		}
    		update(true);
    		break;
    	case OPTION_3: // INFO
    		showInfo = showInfo ? false : true;
    		update(true);
    		break;
    	case OPTION_4: // INTERACTIVE
    		interactive = interactive ? false : true;
    		update(true);
    		break;
    	case OPTION_5: // TRACKER 1
    		tracker1 = true;
    		tracker2 = false;
    		update(true);
    		break;
    	case OPTION_6: // TRACKER 2
    		tracker1 = false;
    		tracker2 = true;
    		update(true);
    		break;
    	}
    	return true;
	}
}
